﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Communication;
using Clipper.Model;
using Global;
using System.Threading;

namespace Clipper.BLL.DetectChain.AddR1AndSample
{
    public class KitA : CupComponent
    {
        //试剂盘A
        public static ReagentKitControl Kit1 = AddReagent1AndSample.Kit1;

        static ControlStatusEnum detectionStatus;
        /// <summary>
        /// 模块状态
        /// </summary>
        public override Model.ControlStatusEnum DetectionStatus
        {
            get
            {
                return detectionStatus;
            }
            set
            {
                detectionStatus = value;
            }
        }
        static ReactionCup mLastCup = null;//上一个反应杯
        int mReagID;
        byte mHoleNum;
        private Clipper.DAL.Reagent mReagent = new DAL.Reagent();               // 试剂信息表
        private Clipper.DAL.ReagentKit mReagentKit = new DAL.ReagentKit();      // 试剂盘信息表
        /// <summary>
        /// 控制样本系统
        /// </summary>
        public static SampleControl Sample = new SampleControl();

        public override void Run(Model.ReactionCup cup)
        {
            DetectionStatus = ControlStatusEnum.BUSY;
            mReagID = mReagent.GetReagentID(cup.DetectItem, 1);
            if (mReagID > 0)
            {
                mHoleNum = mReagentKit.GetHoleNumber(mReagID, 1);
                mHoleNum = 1;//调试用固定为1
                Kit1.Position(mHoleNum);
                if (mLastCup != null && mLastCup.SampleInfoID != cup.SampleInfoID)//需要更换试管
                {
                    if (cup.TestTubeShelfBarcode == mLastCup.TestTubeShelfBarcode)//还在同一个试管架
                    {
                        Sample.WaitStatus(Sample.Position());
                    }
                    else //需要更换试管架 
                    {
                        // if (ServiceProvider.gReactionStripList.Count > 0)
                        // {
                        Sample.WaitStatus(Sample.ChangeShelf());
                        Sample.WaitStatus(Sample.Position());
                        //mdtInStart = DateTime.Now;
                        ServiceProvider.Monitor.InShelfCount--;
                        ServiceProvider.Monitor.OutShelfCount++;
                        ServiceProvider.Monitor.RunningTestTubeShelf = ServiceProvider.WorkSheetVM.TestTubeShelfList[ServiceProvider.Monitor.OutShelfCount];
                        //mdtInStart = DateTime.Now.AddSeconds(10 * ServiceProvider.Monitor.RunningTestTubeShelf.DetectItemCount);
                        //mTimerCountDown.Start();
                        //}
                    }
                }
                mLastCup = cup;
                Kit1.WaitStatus(1);
            }
            DetectionStatus = ControlStatusEnum.IDLE;
            while (true)
            {
                if (NextComponent.DetectionStatus == ControlStatusEnum.IDLE)
                    break;
                else
                    Thread.Sleep(100);
            }
            NextComponent.Run(cup);
        }
    }
}
